<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品型号" prop="modelNumber">
        <el-input
          v-model="queryParams.modelNumber"
          placeholder="请输入产品型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧凑编码" prop="productCodeShort" v-show="false">
        <el-input
          v-model="queryParams.productCodeShort"
          placeholder="请输入紧凑编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品组" prop="productGroup">
        <el-select v-model="queryParams.productGroup" placeholder="请选择产品组" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品品牌" prop="productBrand">
        <el-input
          v-model="queryParams.productBrand"
          placeholder="请输入产品品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="海关编码HS" prop="customNumber" >
        <el-input
          v-model="queryParams.customNumber"
          placeholder="请输入海关编码HS"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品单价人民币" prop="productPrice" v-show="false">
        <el-input
          v-model="queryParams.productPrice"
          placeholder="请输入产品单价人民币"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小起购量" prop="minOrderAmount" v-show="false">
        <el-input
          v-model="queryParams.minOrderAmount"
          placeholder="请输入最小起购量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="repertoryAmount" v-show="false">
        <el-input
          v-model="queryParams.repertoryAmount"
          placeholder="请输入库存"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择产品状态" clearable size="small">
          <el-option label="上架" value="0" />
          <el-option label="下架" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品折扣" prop="discountStatus">
        <el-select v-model="queryParams.discountStatus" placeholder="请选择产品折扣" clearable size="small">
          <el-option label="参与折扣" value="1" />
          <el-option label="不参与折扣" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="折扣率" prop="discount" v-show="false">
        <el-input
          v-model="queryParams.discount"
          placeholder="请输入折扣率"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作员id" prop="operaterId" v-show="false">
        <el-input
          v-model="queryParams.operaterId"
          placeholder="请输入操作员id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:product:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="产品id" align="center" prop="productId" />-->
      <el-table-column label="名称" align="center" prop="productName" />
      <!--<el-table-column label="产品详情介绍" align="center" prop="productDetail" v-show="false"/>-->
      <el-table-column label="产品型号" align="center" prop="modelNumber" />
      <!--<el-table-column label="产品编码" align="center" prop="productCode" />-->
      <el-table-column label="紧凑编码" align="center" prop="productCodeShort" />
      <el-table-column label="产品组" align="center" prop="productGroup" />
      <el-table-column label="产品品牌" align="center" prop="productBrand" />
      <el-table-column label="海关编码HS" align="center" prop="customNumber" />
      <el-table-column label="产品单价￥" align="center" prop="productPrice" />
      <el-table-column label="产品重量kg" align="center" prop="productWeight" />
      <el-table-column label="最小起购量" align="center" prop="minOrderAmount" />
      <el-table-column label="库存" align="center" prop="repertoryAmount" />
      <el-table-column label="产品状态" align="center" prop="status" />
      <el-table-column label="产品折扣" align="center" prop="discountStatus" />
      <el-table-column label="折扣率" align="center" prop="discount" />
      <!--<el-table-column label="操作员id" align="center" prop="operaterId" />-->
      <!--<el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="产品名称" prop="productName" :label-width="formLabelWidth" style="width: 98%;">
              <el-input v-model="form.productName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品型号" prop="modelNumber"  :label-width="formLabelWidth" style="width: 95%;">
              <el-input v-model="form.modelNumber" placeholder="请输入产品型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品编码" prop="productCode"   :label-width="formLabelWidth" style="width: 95%;">
              <el-input v-model="form.productCode" placeholder="请输入产品编码" />
            </el-form-item>
          </el-col>
            <!--<el-form-item label="紧凑编码" prop="productCodeShort"  :label-width="formLabelWidth" style="width: 45%;">-->
            <!--<el-input v-model="form.productCodeShort" placeholder="请输入紧凑编码" />-->
            <!--</el-form-item>-->
          <el-col :span="12">
            <el-form-item label="产品组" prop="productGroup" style="width: 95%;">
              <el-select v-model="form.productGroup" placeholder="请选择产品组">
                <el-option label="S110" value="S110" />
                <el-option label="S312" value="S312" />
                <el-option label="S350" value="S350" />
                <el-option label="S650" value="S650" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品品牌" prop="productBrand"  :label-width="formLabelWidth" style="width: 95%;">
              <el-input v-model="form.productBrand" placeholder="请输入产品品牌" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="海关编码HS" prop="customNumber" :label-width="formLabelWidth" style="width: 95%;">
              <el-input v-model="form.customNumber" placeholder="请输入海关编码HS" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品单价￥" prop="productPrice" :label-width="formLabelWidth" style="width: 95%;">
              <el-input v-model="form.productPrice" placeholder="请输入产品单价人民币" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最小起购量" prop="minOrderAmount" :label-width="formLabelWidth" style="width: 95%;">
              <el-input v-model="form.minOrderAmount" placeholder="请输入最小起购量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品重量kg" prop="productWeight" :label-width="formLabelWidth" style="width: 95%;">
              <el-input v-model="form.minOrderAmount" placeholder="请输入产品重量kg" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品库存" prop="repertoryAmount" :label-width="formLabelWidth" style="width: 95%;">
              <el-input v-model="form.repertoryAmount" placeholder="请输入库存" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品折扣" :label-width="formLabelWidth" style="width: 95%;">
              <el-radio-group v-model="form.discountStatus">
                <el-radio label="1">有折扣</el-radio>
                <el-radio label="0">没有折扣</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="折扣率" prop="discount" :label-width="formLabelWidth" >
              <el-input v-model="form.discount" placeholder="请输入折扣率" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品状态" :label-width="formLabelWidth" style="width: 95%;">
              <el-radio-group v-model="form.status">
                <el-radio label="0">上架</el-radio>
                <el-radio label="1">下架</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
            <!--<el-form-item label="操作员id" prop="operaterId">-->
            <!--<el-input v-model="form.operaterId" placeholder="请输入操作员id" />-->
            <!--</el-form-item>-->
          <el-col :span="24">
            <el-form-item label="产品详情" style="width: 98%;">
              <editor v-model="form.productDetail" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark" :label-width="formLabelWidth" style="width: 98%;">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 产品导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct } from "@/api/system/product";
import Editor from '@/components/Editor';
import { getToken } from "@/utils/auth";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Product",
  components: {
    Editor,
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品信息表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        modelNumber: null,
        productCode: null,
        productCodeShort: null,
        productGroup: null,
        productBrand: null,
        customNumber: null,
        productPrice: null,
        minOrderAmount: null,
        repertoryAmount: null,
        status: null,
        discountStatus: null,
        discount: null,
        operaterId: null,
      },
      // 表单参数
      form: {},
      // 产品导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/product/importData"
      },
      // 表单校验
      rules: {
        productName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        productCode: [
          { required: true, message: "产品编码不能为空", trigger: "blur" }
        ],
        modelNumber: [
          { required: true, message: "产品型号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品信息列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productName: null,
        productDetail: null,
        modelNumber: null,
        productCode: null,
        productCodeShort: null,
        productGroup: null,
        productBrand: null,
        customNumber: null,
        productPrice: null,
        minOrderAmount: null,
        productWeight: null,
        repertoryAmount: null,
        status: "0",
        discountStatus: "0",
        discount: null,
        createTime: null,
        updateTime: null,
        operaterId: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getProduct(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            updateProduct(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$confirm('是否确认删除产品信息编号为"' + productIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProduct(productIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProduct(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "产品导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {debugger
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
